//

#include "staticimage.hpp"

namespace KEG
{

bool StaticImage::LoadFullImage( const std::string& str )
{
	if( !texture.Load(str) )
		return false;
	texCoord[0] = 0;
	texCoord[1] = 0;
	texCoord[2] = texture.Width;
	texCoord[3] = texture.Height;
	screenWidth = texture.Width;
	screenHeight = texture.Height;
	return true;
}

void StaticImage::Release()
{
	texture.Release();
}

void StaticImage::Render( float x, float y ) const
{
	glBindTexture( GL_TEXTURE_RECTANGLE_ARB, texture.Tex );
	glBegin( GL_QUADS );

	glTexCoord2f(texCoord[0],texCoord[1]); glVertex2f(x,y);
	glTexCoord2f(texCoord[2],texCoord[1]); glVertex2f(x+screenWidth, y);
	glTexCoord2f(texCoord[2],texCoord[3]); glVertex2f(x+screenWidth, y+screenHeight);
	glTexCoord2f(texCoord[0],texCoord[3]); glVertex2f(x, y+screenHeight);

	glEnd();
}

void StaticImage::RenderPartial( float startx, float starty, float endx, float endy, float x, float y ) const
{
	glBindTexture( GL_TEXTURE_RECTANGLE_ARB, texture.Tex );
	glBegin( GL_QUADS );

	// note: assumes texture pixel corresponds to screen pixel
	// ie 1:1 ratio - no scaling
	float width = endx - startx;
	float height = endy - starty;

	glTexCoord2f(startx,starty); glVertex2f(x,y);
	glTexCoord2f(endx,starty); glVertex2f(x+width, y);
	glTexCoord2f(endx,endy); glVertex2f(x+width, y+height);
	glTexCoord2f(startx,endy); glVertex2f(x, y+height);

	glEnd();
}

void StaticImage::RenderPartial( float startx, float starty, float x, float y ) const
{
	RenderPartial( startx, starty, texCoord[2], texCoord[3], x, y );
}

void StaticImage::RenderOrigin() const
{
	Render( -screenWidth/2, -screenHeight/2 );
}

void StaticImage::RenderCentered( float totalwidth, float totalheight ) const
{
	Render( totalwidth/2 - screenWidth/2, totalheight/2 - screenHeight/2 );
}

void StaticImage::RenderCenteredX( float y, float totalwidth ) const
{
	Render( totalwidth/2 - screenWidth/2, y );
}

void StaticImage::RenderCenteredY( float x, float totalheight ) const
{
	Render( x, totalheight/2 - screenHeight/2 );
}

}
